package com.example.project.projectspringcloud.service.impl.order;

import com.example.project.projectspringcloud.common.OperationException;
import com.example.project.projectspringcloud.dao.order.OrderReceiptMapper;
import com.example.project.projectspringcloud.entity.Page;
import com.example.project.projectspringcloud.entity.order.OrderReceipt;
import com.example.project.projectspringcloud.service.BaseService;
import com.example.project.projectspringcloud.service.order.OrderReceiptService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class OrderReceiptServiceImpl extends BaseService<OrderReceipt, OrderReceiptMapper> implements OrderReceiptService {

    @Override
    public OrderReceipt get(OrderReceipt orderReceipt) {
        return null;
    }

    @Override
    public List<OrderReceipt> list(Page page, OrderReceipt orderReceipt) {
        return null;
    }

    @Override
    public boolean save(OrderReceipt orderReceipt) {
        return super.save(orderReceipt);
    }

    @Override
    public boolean create(OrderReceipt orderReceipt) throws OperationException {

        if (orderReceipt.getOrder() == null) {
            throw new OperationException("未关联订单");
        }

        if (orderReceipt.getType() == null) {
            throw new OperationException("请选择发票类型");
        }

        if (orderReceipt.getTitle() == null) {
            throw new OperationException("请选择发票抬头类型");
        }

        // 如果发票抬头类型是个人
        if (orderReceipt.getTitle().equals(1)) {

        }
        // 如果发票抬头类型是公司
        else if (orderReceipt.getTitle().equals(2)) {
            if (StringUtils.isBlank(orderReceipt.getUnitName())) {
                throw new OperationException("请输入公司名称");
            }

            if (StringUtils.isBlank(orderReceipt.getDutyNo())) {
                throw new OperationException("请输入纳税人识别号");
            }
        }

        /**
         * 收票人信息
         */
        if (StringUtils.isBlank(orderReceipt.getReceiveName())) {
            throw new OperationException("请输入收票人名称");
        }

        if (StringUtils.isBlank(orderReceipt.getContactPhone())) {
            throw new OperationException("请输入收票人联系电话");
        }

        // 纸质
        if (orderReceipt.getType().equals(1)) {
            if (StringUtils.isBlank(orderReceipt.getPostStreet())) {
                throw new OperationException("请输入收票地址");
            }
        }
        // 电子
        else if (orderReceipt.getType().equals(2)) {
            if (StringUtils.isBlank(orderReceipt.getContactEmail())) {
                throw new OperationException("请输入收票邮箱");
            }
        }

        orderReceipt.setStatus(1);

        if (super.save(orderReceipt)) {
            return true;
        }

        return false;
    }

    @Override
    public boolean delete(Integer[] ids) {
        return false;
    }
}
